package com.lu.controller;
import com.lu.mapper.DepartmentDao;
import com.lu.mapper.TeacherMapper;
import com.lu.pojo.Department;
import com.lu.pojo.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * Created by LWC
 * Date 2021/7/29 16:57
 * Description
 */
@Controller
public class TeacherController {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Teacher> teacherList = teacherMapper.queryTeacherList();
        model.addAttribute("emps",teacherList);
        return "emp/list";
    }

    //转跳到添加页面
    @GetMapping("/emp")
    public String addEmp(Model model){
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    //添加属性
    @PostMapping("/emp")
    public String add(Teacher teacher){
        teacherMapper.addTeacher(teacher);
        return "redirect:/emps";
    }

    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id")Integer id,Model model){
        Teacher teacher = teacherMapper.queryTeacherById(id);
        model.addAttribute("emp",teacher);
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    @GetMapping("/deleteTeacher/{id}")
    public String deleteUser(@PathVariable("id") int id){
        teacherMapper.deleteTeacher(id);
        return "redirect:/emps";
    }

    @PostMapping("/updateEmp")
    public String updateEmp(Teacher teacher){
        teacherMapper.updateTeacher(teacher);
        return "redirect:/emps";
    }

}
